#include "serial.h"
#include "asmfunc.h"
#include "kasm.h"


//SOME CONSTANTS FOR PROGRAMMING THE UART
#define LOOPBACK	0x10
#define OUT2		0x08

int port_in_use=0;


void
write_UART(int comPort, int reg, int data)
{
	outpw((comPort + reg), data);

}

int 
read_UART(int comPort, int reg)
{
	int resp;
	inpw(comPort + reg, &resp);	

	return resp;
}

int
setup_serial(int comPort, int baud, unsigned char misc)
{	
	short divisor;

	
	write_UART(comPort, REG_LCR, (int) DLR_ON);
	divisor = 0x1c200/baud;
	outpw(comPort, divisor);

	write_UART(comPort, REG_LCR, (int) misc);

	write_UART(comPort, REG_MCR, (int) OUT2); //loopback && OUT2 =24


	write_UART(comPort, REG_IER, 1); /* habilito la interrupcion de rx*/
	return 1;
/* http://heim.ifi.uio.no/~stanisls/helppc/8250.html */
}

 
void
serial_write (int comPort, unsigned char ch)
{
	
	while(!(read_UART(comPort, REG_LSR) & 0x20));


	_Cli();

	write_UART(comPort, REG_THR, (int)ch);

	_Sti();

}

unsigned char
simple_serial_read(int comPort)
{
	//while(!((unsigned char) read_UART(comPort, REG_LSR)<<7));

	return read_UART(comPort, REG_RHR);

}

